from django.http import JsonResponse
from django.views.generic import View
from ..utils.literUtils import get_model_serializer_config, type_conversion_dict
from ..serializers.globalTableListSerializer import GlobalTableListQuerySerializer, GlobalTableListConfigSerializer
from ..serializers.globalTableInfoSerializer import GlobalTableCreateSerializer

class GlobalTableConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = GlobalTableListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(GlobalTableListConfigSerializer)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            sortable_fields = ["name", "zh_name"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
            # print(table_fields)
            # 获取展示字段
            data["table_fields"] = table_fields
            # print(data)
            res["code"] = 200
            res["data"] = data
            res["message"] = "global table配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

# 新建界面配置
class GlobalTableAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(GlobalTableCreateSerializer)
            hidden_fields = []      
            
            for field in add_fields:
                # print(field['dataIndex'] ,field['type'])
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)